package cn.melonlib.cores.graphql.comp.config.impl;

import cn.melonlib.cores.graphql.comp.config.ScalarTypeConfig;
import cn.melonlib.cores.graphql.model.ext.scalars.*;
import cn.melonlib.cores.jpa.model.bean.collections.DataMap;
import cn.melonlib.cores.jpa.model.bean.collections.JsonMap;
import graphql.schema.GraphQLScalarType;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.support.ApplicationObjectSupport;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.io.File;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Component("defaultScalarTypeConfig")
@ConditionalOnProperty(value = "system.worker.config.init.scalar",havingValue = "default",matchIfMissing = true)
public class DefaultScalarTypeConfig extends ApplicationObjectSupport implements ScalarTypeConfig {

    private List<GraphQLScalarType> scalarTypes;

    private JsonMap<GraphQLScalarType> scalarsOfName;

    private DataMap<Class,GraphQLScalarType> scalarOfClass;

    @Resource
    private GraphQLScalarType dateType;

    @Resource
    private GraphQLScalarType viewEntityType;

    @Resource
    private GraphQLScalarType filterType;

    @Resource
    private GraphQLScalarType inputType;

    @Resource
    private GraphQLScalarType listType;

    @Resource
    private GraphQLScalarType byteDataType;

    @Resource
    private GraphQLScalarType fileType;

    @Resource
    private GraphQLScalarType errorType;

    @Resource
    private GraphQLScalarType messageType;

    @Override
    @PostConstruct
    public void init() {
        scalarsOfName=defaultNameScalarType()
                .append("Date",dateType)
                .append("ViewEntity",viewEntityType)
                .append("Filter",filterType)
                .append("InputEntity",inputType)
                .append("ArrayList",listType)
                .append("ByteData",byteDataType)
                .append("File",fileType)
                .append("Error",errorType)
                .append("Message",messageType);
        scalarOfClass=defaultClassScalarType()
                .append(Date.class,dateType)
                .append(ViewEntityType.class,viewEntityType)
                .append(FilterType.class,filterType)
                .append(InputEntityType.class,inputType)
                .append(List.class,listType)
                .append(BytesType.class,byteDataType)
                .append(File.class,fileType)
                .append(ErrorType.class,errorType)
                .append(MessageType.class,messageType);
        scalarTypes=scalarsOfName.entrySet().stream().map(Map.Entry::getValue).collect(Collectors.toList());
    }

    @Override
    public List<GraphQLScalarType> allScalarTypes() {
        return scalarTypes;
    }

    @Override
    public GraphQLScalarType getType(Class typeCls) {
        return scalarOfClass.get(typeCls);
    }

    @Override
    public GraphQLScalarType getType(String typeName) {
        return scalarsOfName.get(typeName);
    }
}
